namespace BM.Tools.WebControls.MForm.Controls.Helper
{
    using System.Web.UI;

    /// <summary>
    /// A simple web control that marks where more items of the given <see cref="Offspring"/> may be placed in html.
    /// </summary>
    public class Marker : Control
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Marker"/> class. 
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public Marker(Offspring owner)
        {
            Owner = owner;
        }

        /// <summary>
        /// Gets Owner.
        /// </summary>
        /// <value>
        /// The owner.
        /// </value>
        public Offspring Owner
        {
            get; 
            private set;
        }

        /// <summary>
        /// Gets or sets Visible.
        /// </summary>
        /// <value>
        /// The visible.
        /// </value>
        public override bool Visible
        {
            get
            {
                if (Owner.MinOccurs == Owner.MaxOccurs)
                {
                    return false;
                }

                if (Owner is Leaf && Owner.MaxOccurs == 1)
                {
                    return false;
                }

                return base.Visible;
            }

            set
            {
                base.Visible = value;
            }
        }

        /// <summary>
        /// Renders the marker
        /// </summary>
        /// <param name="writer">
        /// The writer.
        /// </param>
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<!--");
            writer.Write(Owner.Name);
            writer.Write("-->");
        }
    }
}